package com.sinya.shoppingmall;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sinya.shoppingmall.bean.BaseResult;
import com.sinya.shoppingmall.bean.OrderBean;
import com.sinya.shoppingmall.https.VolleyParams;
import com.sinya.shoppingmall.https.VolleyRequest;
import com.sinya.shoppingmall.utils.IntoUtils;
import com.sinya.shoppingmall.widget.timeline.TimeLineAdapter;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class MyOrderListActivity extends AppCompatActivity {


    //订单类型
    private int orderType;

    private Context context;
    private RecyclerView recyclerView;
    private TimeLineAdapter mTimeLineAdapter;

    //返回订单列表
    public List<OrderBean> orderBeans;

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {

                case 0:
                    if (msg.obj != null) {
                        orderBeans = ((BaseResult) msg.obj).orderList;

                        Collections.sort(orderBeans, new Comparator<OrderBean>() {
                            public int compare(OrderBean arg0, OrderBean arg1) {
                                return arg1.time.compareTo(arg0.time);
                            }
                        });

                        for (OrderBean bean : orderBeans) {

                            //避免重复添加
                            if (!orderBeans.contains(bean)) {
                                orderBeans.add(0, bean);
                            }
                        }

                        //刷新数据   ??疑问：为什么直接在这里notifydatasetchanged无法刷新数据??
                        mTimeLineAdapter.updateList(orderBeans);
                    }

                    break;
                default:
                    break;
            }

            return true;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order_list);
        context = this;

        orderType = getData();
        initView();
    }

    private int getData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            //如果没有传值，默认是1
            return orderType = bundle.getInt("OrderType", 2);
        } else {
            //如果bundle为null，默认是1
            return 2;
        }
    }

    private void initView() {
        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("我的订单");

        ImageView back = (ImageView) findViewById(R.id.img_back);
        back.setImageResource(R.drawable.btn_title_back_selector);
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        recyclerView = (RecyclerView) findViewById(R.id.timeline_recyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());//默认动画
        recyclerView.setHasFixedSize(true);//效率最高*/

        getDataFromNet();
        setView();

    }

    @Override
    protected void onResume() {
        getDataFromNet();
        super.onResume();
    }

    //网路请求数据
    private void getDataFromNet() {

        if (orderType == 1) {
            //显示近一个月订单
            VolleyRequest.Start(this, handler, 0, VolleyParams.orderList, new String[]{"20428", "1", "0", "10"});

        } else if (orderType == 2) {
            //显示一个月前订单
            VolleyRequest.Start(this, handler, 0, VolleyParams.orderList, new String[]{"20428", "2", "0", "10"});
        } else if (orderType == 3) {
            //显示退货单
            VolleyRequest.Start(this, handler, 0, VolleyParams.orderList, new String[]{"20428", "3", "0", "10"});
        }

    }

    private void setView() {

        mTimeLineAdapter = new TimeLineAdapter(MyOrderListActivity.this, orderBeans);
        recyclerView.setAdapter(mTimeLineAdapter);
        mTimeLineAdapter.setOnItemClickListener(new TimeLineAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, Object data) {

                //TODO 跳转到订单详情页面
                Bundle bundle = new Bundle();
                OrderBean orderBean = (OrderBean) data;
                bundle.putString("userid", "20428");
                bundle.putString("orderId", orderBean.orderId);
                IntoUtils.goActivity(context, bundle, OrderDetailActivity.class);
            }
        });

    }

}